﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Tour_TourDestinationMan : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            SetLabelLanguage();
            ShowMess();
            BindDataToLanguageDropDown();
            BindDataToCountryDropdown();
            BindDataToRegionDropdown(0);
            BindDataToGrid(1);

        }
    }
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        btnBack.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage - 1];
        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefaultLanguage - 1];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        lblCountry.Text = MultipleLanguage.LBL_COUNTRY[nDefaultLanguage - 1];
        lblRegion.Text = MultipleLanguage.LBL_REGION[nDefaultLanguage - 1];
        
        btnSearch.Text = MultipleLanguage.BTN_SEARCH[nDefaultLanguage-1];
    }
    #region Paging & Message
    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = Config.PAGESIZE;
        if (intPagecount > nPageSize)
            Pager1.Visible = true;
        else
            Pager1.Visible = false;
    }
    public void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndx;
        BindDataToGrid(currnetPageIndx);
    }
    private void initDataPaging()
    {
        Pager1.PageSize = Config.PAGESIZE;
        Pager1.ShowFirstLast = true;
        Pager1.FirstClause = "Đầu";
        Pager1.LastClause = "Cuối";
        Pager1.NextClause = "Tiếp";
        Pager1.OfClause = "của";
        Pager1.PageClause = "Trang";
        Pager1.CompactedPageCount = 5;
        Pager1.PreviousClause = "Sau";
        Pager1.MaxSmartShortCutCount = 5;
        Pager1.NotCompactedPageCount = 5;
    }

    private void ShowMess()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaultLanguage];
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaultLanguage]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    #endregion

    #region BindDataToDropDown
    private void BindDataToCountryDropdown()
    {
        DataTable dtbCountry = new DataTable();
        Country objCountry = new Country();
        dtbCountry = objCountry.GetALL();

        ddlCountry.DataSource = dtbCountry;
        ddlCountry.DataValueField = "CountryID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlCountry.DataTextField = "CountryName1";
                break;
            case 2:
                ddlCountry.DataTextField = "CountryName2";
                break;
            case 3:
                ddlCountry.DataTextField = "CountryName3";
                break;
        }
        ddlCountry.DataBind();
        Utl.SetNonSelectToControl(ddlCountry, MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
    }
    private void BindDataToRegionDropdown(int nCountry)
    {
        DataTable dtbRegion = new DataTable();
        Region objRegion = new Region();
        dtbRegion = objRegion.SearchByCountry(nCountry);
        ddlRegion.DataSource = dtbRegion;
        ddlRegion.DataValueField = "RegionID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlRegion.DataTextField = "RegionName1";
                break;
            case 2:
                ddlRegion.DataTextField = "RegionName2";
                break;
            case 3:
                ddlRegion.DataTextField = "RegionName3";
                break;
        }
        ddlRegion.DataBind();
        Utl.SetNonSelectToControl(ddlRegion, MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
    }
    private void BindDataToGrid(int CurrentPageIndex)
    {
        TourDestination objTourDestination = new TourDestination();
        DataTable dtbTourDestination = new DataTable();
        int nRegion = ConvertData.ConvertToInt(ddlRegion.SelectedValue);
        int nCountry = ConvertData.ConvertToInt(ddlCountry.SelectedValue);
        dtbTourDestination = objTourDestination.Search(nRegion, nCountry);
        dtgTourDestination.DataSource = dtbTourDestination;
        dtgTourDestination.DataBind();
        int nDefaultLanguage = Support.GetDefaultLanguage();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage - 1] + dtbTourDestination.Rows.Count + MultipleLanguage.LBL_FOUND[nDefaultLanguage - 1];
        Pager1.ItemCount = dtbTourDestination.Rows.Count;

        VisiblePaging(dtbTourDestination.Rows.Count);
        if (dtbTourDestination.Rows.Count > 0)
            tblWarning.Visible = false;
        else
            tblWarning.Visible = true;
    }
    protected void dtgTourDestination_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        TourDestination objTourDestination = new TourDestination();
        switch (strCommand)
        {
            case "Delete":
                objTourDestination.LoadById(nID);
                Support.DeleteFile("tourdestination", objTourDestination.Data.Image);
                int nDelete = objTourDestination.DeleteById(nID);
                if (nDelete > 0)
                    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLanguage]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLanguage]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;
            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURDESTINATION_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;
        }
    }
    protected void dtgTourDestination_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblTourDestinationTitle = (Label)e.Item.FindControl("lblTourDestinationTitle");
            Label lblRegion = (Label)e.Item.FindControl("lblRegion");
            Label lblCountry = (Label)e.Item.FindControl("lblCountry");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");

            Region objRegion = new Region();
            objRegion.LoadById(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "RegionID")));
            Country objCountry = new Country();
            objCountry.LoadById(objRegion.Data.CountryID);

            switch (nCurrentLanguage)
            {
                case 1:
                    lblTourDestinationTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TourDestinationTitle1"));
                    lblRegion.Text = ConvertData.ConvertToString(objRegion.Data.RegionName1);
                    lblCountry.Text = ConvertData.ConvertToString(objCountry.Data.CountryName1);
                    break;
                case 2:
                    lblTourDestinationTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TourDestinationTitle2"));
                    lblRegion.Text = ConvertData.ConvertToString(objRegion.Data.RegionName2);
                    lblCountry.Text = ConvertData.ConvertToString(objCountry.Data.CountryName2);
                    break;
                case 3:
                    lblTourDestinationTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TourDestinationTitle3"));
                    lblRegion.Text = ConvertData.ConvertToString(objRegion.Data.RegionName3);
                    lblCountry.Text = ConvertData.ConvertToString(objCountry.Data.CountryName3);
                    break;
            }
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TourDestinationID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaultLanguage - 1]);
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TourDestinationID"));
        }
    }
    #endregion

    #region Grid processing
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataToCountryDropdown();
        BindDataToRegionDropdown(0);
        BindDataToGrid(1);
    }
    #endregion

    #region Click Events
    protected void btnAddnew_Click1(object sender, EventArgs e)
    {
        string sAddNew = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURDESTINATION_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAddNew);
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOUR);
    }
    #endregion
    
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nCountry = ConvertData.ConvertToInt(ddlCountry.SelectedValue);
        BindDataToRegionDropdown(nCountry);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindDataToGrid(1);
    }
}